/*
 *  PLJ 自定义用户设置信息
 *  2024年3月21日
 */
'use strict';

angular.module('flowableModeler').controller('FlowableUserCtrl', ['$scope', '$modal', function ($scope, $modal) {

    // Config for the modal window
    var opts = {
        template: 'editor-app/configuration/properties/user-popup.html?version=' + Date.now(),
        scope: $scope
    };

    // Open the dialog
    _internalCreateModal(opts, $modal, $scope);
}]);

angular.module('flowableModeler').controller('FlowableUserPopupCtrl',
    ['$rootScope', '$scope', '$translate', '$http', 'UserService', 'GroupService', function ($rootScope, $scope, $translate, $http, UserService, GroupService) {

        // 关闭按钮
        $scope.close = function () {
            $scope.property.mode = 'read';
            $scope.$hide();
        };
        // 保存按钮
        $scope.save = function () {
            // 处理数据
            console.log("保存数据...");
            /**
             * type: idm 身份存储,static 固定值，一般是用${}
             * initiatorCanCompleteTask: 允许流程发起人完成任务
             * */
            $scope.assignment.type = "idm";
            var idm = {type: $scope.assignmentOption.id};
            $scope.assignment.idm = idm;
            // 用户数据
            if ($scope.userSelectData && $scope.userSelectData.length > 0) {
                idm.candidateUsers = $scope.userSelectData;
            }
            // 部门数据
            if ($scope.deptSelectData && $scope.deptSelectData.length > 0) {
                idm.candidateGroups = $scope.deptSelectData;
            }
            // 岗位数据
            if ($scope.postSelectData && $scope.postSelectData.length > 0) {
                idm.candidateGroups = $scope.postSelectData;
            }
            // 岗位组数据
            if ($scope.groupSelectData && $scope.groupSelectData.length > 0) {
                idm.candidateGroups = $scope.groupSelectData;
            }
            // 岗位等级数据
            if ($scope.levelSelectData && $scope.levelSelectData.length > 0) {
                idm.candidateGroups = $scope.levelSelectData;
            }
            // 保存数据
            $scope.property.value = {};
            $scope.property.value.assignment = $scope.assignment;
            $scope.updatePropertyInModel($scope.property);

            $scope.close();
        }

        // 初始化静态资源
        initStaticResource($scope);

        function initStaticResource($scope) {
            console.log("初始化用户信息数据......");
            $scope.allocationOperations = [
                {id: 1, type: "user", title: "用户"},
                {id: 2, type: "dept", title: "部门"},
                {id: 3, type: "post", title: "岗位"},
                {id: 4, type: "group", title: "岗位组"},
                {id: 5, type: "level", title: "岗位等级"}
            ];
            // 默认值
            $scope.allocationSelect = {};
            $scope.tableLoading = false;// 表格数据是否加载完成
            // 用户数据
            $scope.userTableData = [];
            $scope.userSelectData = [];
            // 部门数据
            $scope.deptTableData = [];
            $scope.deptSelectData = [];
            // 岗位数据
            $scope.postTableData = [];
            $scope.postSelectData = [];
            // 岗位组数据
            $scope.groupTableData = [];
            $scope.groupSelectData = [];
            // 岗位等级数据
            $scope.levelTableData = [];
            $scope.levelSelectData = [];

            // 初始化
            $scope.assignmentOptions = [
                {id: "initiator", title: $translate.instant('PROPERTY.ASSIGNMENT.IDM.DROPDOWN.INITIATOR')},
                {id: "user", title: $translate.instant('PROPERTY.ASSIGNMENT.IDM.DROPDOWN.USER')},
                {id: "users", title: $translate.instant('PROPERTY.ASSIGNMENT.IDM.DROPDOWN.USERS')},
                {id: "groups", title: $translate.instant('PROPERTY.ASSIGNMENT.IDM.DROPDOWN.GROUPS')}
            ];
            $scope.assignment = {} //用于存放用户信息数据
        }

        $scope.allocationChange = function () {
            if ($scope.allocationSelect) {
                $scope.allocation = $scope.allocationSelect.type;
            } else {
                $scope.allocation = "-1";
            }
            if ($scope.allocation === 'user') {
                console.log("设置用户");
                $scope.tableLoading = false;
                var url = localStorage.getItem("baseURL") + "/zksz-auth/user/findAllList";
                $http({
                    method: 'GET',
                    url: url,
                    headers: {
                        accessToken: localStorage.getItem("accessToken")
                    }
                }).then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        $scope.userTableData = data.result.map(user => {
                            user.isChecked = false;
                            return user;
                        });
                        $scope.tableLoading = true;
                    }
                }, function (error) {
                    console.log("请求失败的回调函数 ", error);
                });
                $scope.assignmentOption = $scope.assignmentOptions[2];
            } else if ($scope.allocation === 'dept') {
                console.log("设置部门");
                $scope.tableLoading = false;
                var url = localStorage.getItem("baseURL") + "/zksz-auth/depart/findList";
                $http({
                    method: 'GET',
                    url: url,
                    headers: {
                        accessToken: localStorage.getItem("accessToken")
                    }
                }).then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        $scope.deptTableData = data.result.map(dept => {
                            dept.isChecked = false;
                            return dept;
                        });
                        $scope.tableLoading = true;
                    }
                }, function (error) {
                    console.log("请求失败的回调函数 ", error);
                });
                $scope.assignmentOption = $scope.assignmentOptions[3];
            } else if ($scope.allocation === 'post') {
                console.log("设置岗位");
                $scope.tableLoading = false;
                var url = localStorage.getItem("baseURL") + "/zksz-auth/position/findList";
                $http({
                    method: 'GET',
                    url: url,
                    headers: {
                        accessToken: localStorage.getItem("accessToken")
                    }
                }).then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        $scope.postTableData = data.result.map(post => {
                            post.isChecked = false;
                            return post;
                        });
                        $scope.tableLoading = true;
                    }
                }, function (error) {
                    console.log("请求失败的回调函数 ", error);
                });
                $scope.assignmentOption = $scope.assignmentOptions[3];
            } else if ($scope.allocation === 'group') {
                console.log("设置岗位组");
                $scope.tableLoading = false;
                var url = localStorage.getItem("baseURL") + "/zksz-auth/positionGroup/findList";
                $http({
                    method: 'GET',
                    url: url,
                    headers: {
                        accessToken: localStorage.getItem("accessToken")
                    }
                }).then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        $scope.groupTableData = data.result.map(group => {
                            group.isChecked = false;
                            return group;
                        });
                        $scope.tableLoading = true;
                    }
                }, function (error) {
                    console.log("请求失败的回调函数 ", error);
                });
                $scope.assignmentOption = $scope.assignmentOptions[3];
            } else if ($scope.allocation === 'level') {
                console.log("设置等级");
                $scope.tableLoading = false;
                var url = localStorage.getItem("baseURL") + "/zksz-auth/positionLevel/findList";
                $http({
                    method: 'GET',
                    url: url,
                    headers: {
                        accessToken: localStorage.getItem("accessToken")
                    }
                }).then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        $scope.levelTableData = data.result.map(level => {
                            level.isChecked = false;
                            return level;
                        });
                        $scope.tableLoading = true;
                    }
                }, function (error) {
                    console.log("请求失败的回调函数 ", error);
                });
                $scope.assignmentOption = $scope.assignmentOptions[3];
            } else {
                $scope.allocation === '-1'
                console.log("清空！！！！！！");
                $scope.assignmentOption = $scope.assignmentOptions[0];
            }
        };

        $scope.userClick = function (e) {
            if (e.isChecked) {//选中
                $scope.userSelectData.push(e);
            } else {//取消
                $scope.userSelectData = $scope.userSelectData.filter(item => item.id !== e.id);
            }
            console.log("选中的用户：", $scope.userSelectData);
        };
        $scope.deptClick = function (e) {
            if (e.isChecked) {//选中
                $scope.deptSelectData.push(e);
            } else {//取消
                $scope.deptSelectData = $scope.deptSelectData.filter(item => item.id !== e.id);
            }
            console.log("选中的部门：", $scope.deptSelectData);
        };
        $scope.postClick = function (e) {
            if (e.isChecked) {//选中
                $scope.postSelectData.push(e);
            } else {//取消
                $scope.postSelectData = $scope.postSelectData.filter(item => item.id !== e.id);
            }
            console.log("选中的岗位：", $scope.postSelectData);
        };
        $scope.groupClick = function (e) {
            if (e.isChecked) {//选中
                $scope.groupSelectData.push(e);
            } else {//取消
                $scope.groupSelectData = $scope.groupSelectData.filter(item => item.id !== e.id);
            }
            console.log("选中的组：", $scope.groupSelectData);
        };
        $scope.levelClick = function (e) {
            if (e.isChecked) {//选中
                $scope.levelSelectData.push(e);
            } else {//取消
                $scope.levelSelectData = $scope.levelSelectData.filter(item => item.id !== e.id);
            }
            console.log("选中的等级：", $scope.levelSelectData);
        };
    }]);